<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of quanly
 *
 * @author Bui
 */
class Quanly_LamGiaSu extends CI_Controller {

    private $limit = 10;
    private $nameOfController = 'làm gia sư';

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('table');
        $this->load->library('form_validation');
        $this->load->model('quanly_lamgiasu_model', '', TRUE);
    }

    function index($offset = 0) {
        // offset
        $uri_segment = 3;
        $offset = $this->uri->segment($uri_segment);

        // load data
        $data['result'] = $this->quanly_lamgiasu_model->get_paged_list($this->limit, $offset)->result();

        // generate pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('quanly/quanly_lamgiasu/index/');
        $config['total_rows'] = $this->quanly_lamgiasu_model->count_all();
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        // generate table data
        $this->load->library('table');
        $this->table->set_empty("&nbsp;");
        $this->table->set_heading('ID', 'ID Thành viên', 'Trạng thái', 'ID Thông tin gia sư', 'Năm sinh', 'Nơi sinh', 'Trường học', 'Chuyên ngành', 'Năm tốt nghiệp', 'Hiện là', 'Khối lớp dạy', 'Khu vực dạy', 'Thời gian dạy', 'Số buổi', 'Mức lương yêu cầu', 'Trình độ ID', 'Xem', 'Xóa');
        $i = 0 + $offset;
        foreach ($data['result'] as $row) {
            $this->table->add_row($row->id, $row->thanhvien_id, $row->LamGiaSu_TrangThai_id, $row->ThongTinGiaSu_Id, $row->NamSinh, $row->NoiSinh, 
                    $row->HocTruong, $row->ChuyenNganh, $row->NamTotNghiep, $row->HienLa, $row->KhoiLopDay, $row->KhuVucDay, 
                    $row->ThoiGianDay, $row->SoBuoiDay, $row->MucLuongYeuCau, $row->TrinhDo_id,
                    anchor('quanly/quanly_lamgiasu/view/' . $row->id, ' ', array('class' => 'view')), anchor('quanly/quanly_lamgiasu/delete/' . $row->id, ' ', array('class' => 'delete', 'onclick' => "return confirm('Are you sure want to delete this person?')"))
            );
        }
        $data['table'] = $this->table->generate();

        // load view
        $this->load->view('admin/quanly/quanly_lamgiasu/quanly_lamgiasu_list', $data);
    }

    function add() {
        // set common properties
        $data['title'] = 'Add new data';
        $data['message'] = '';
        $data['action'] = site_url('quanly/quanly_lamgiasu/addData');
        $data['link_back'] = anchor('quanly/quanly_lamgiasu/index', 'Back to list of '.$this->nameOfController, array('class' => 'back'));

        // load view
        $this->load->view('admin/quanly/quanly_lamgiasu/quanly_lamgiasu_add', $data);
    }

    function addData() {
        // set common properties
        $data['title'] = 'Add new data';
        $data['action'] = site_url('quanly/quanly_lamgiasu/addData');
        $data['link_back'] = anchor('quanly/quanly_lamgiasu/index', 'Back to list of '.$this->nameOfController, array('class' => 'back'));
        // set validation properties

        // run validation
        if ($this->form_validation->run() == FALSE) {
            $data['message'] = 'Chưa tạo đc';
            $this->load->view('admin/quanly/quanly_lamgiasu/quanly_lamgiasu_add', $data);
        } else {
            // save data
            $data['message'] = 'Tạo đc';
            $data = array(
                'thanhvien_id' => 1,
                'ThongTinGiaSu_Id' => 0,
                'LamGiaSu_TrangThai_id' => 1,
                'NamSinh' => $this->input->post('NamSinh'),
                'NoiSinh' => $this->input->post('NoiSinh'),
                'HienLa' => $this->input->post('HienLa'),
                'KhoiLopDay' => $this->input->post('KhoiLopDay'),
                'KhuVucDay' => $this->input->post('KhuVucDay'),
                'ThoiGianDay' => $this->input->post('ThoiGianDay'),
                'SoBuoiDay' => $this->input->post('SoBuoiDay'),
                'MucLuongYeuCau' => $this->input->post('MucLuongYeuCau'),
                'TrinhDo_id' => $this->input->post('TrinhDo_id'),
            );
            $id = $this->quanly_lamgiasu_model->add($data);

            // set form input name="id"
            $this->form_validationvalidation->id = $id;
            redirect('quanly/quanly_lamgiasu/index', 'refresh');
        }
    }

    function view($id) {
        // set common properties
        $data['title'] = 'Chi tiết cấp bậc admin';
        $data['link_back'] = anchor('quanly/quanly_lamgiasu/index', 'Back to list of '.$this->nameOfController, array('class' => 'back'));

        // get person details
        $data['row'] = $this->quanly_lamgiasu_model->get_by_id($id)->row();

        // load view
        $this->load->view('admin/quanly/quanly_lamgiasu/quanly_lamgiasu_view', $data);
    }

    function update($id) {
        // prefill form values
        $data['query'] = $this->quanly_lamgiasu_model->get_by_id($id)->row();
        // set common properties
        $data['title'] = 'Update admin level';
        $data['message'] = '';
        $data['action'] = site_url('quanly/quanly_lamgiasu/updateData');
        $data['link_back'] = anchor('quanly/quanly_lamgiasu/index', 'Back to list of '.$this->nameOfController, array('class' => 'back'));

        // load view
        $this->load->view('admin/quanly/quanly_lamgiasu/quanly_lamgiasu_edit', $data);
    }

    function updateData($id) {
        // set common properties
        $data['title'] = 'Update admin level';
        $data['action'] = site_url('quanly/quanly_lamgiasu/updateData');
        $data['link_back'] = anchor('quanly/quanly_lamgiasu/index', 'Back to list of '.$this->nameOfController, array('class' => 'back'));

        // set validation properties

        // run validation
        if ($this->form_validation->run() == FALSE) {
            $data['message'] = '';
            // load view
            $this->load->view('admin/quanly/quanly_lamgiasu/quanly_lamgiasu_edit', $data);
        } else {
            // save data
            $data = array(
                'thanhvien_id' => 1,
                'ThongTinGiaSu_Id' => 0,
                'LamGiaSu_TrangThai_id' => 1,
                'NamSinh' => $this->input->post('NamSinh'),
                'NoiSinh' => $this->input->post('NoiSinh'),
                'HienLa' => $this->input->post('HienLa'),
                'KhoiLopDay' => $this->input->post('KhoiLopDay'),
                'KhuVucDay' => $this->input->post('KhuVucDay'),
                'ThoiGianDay' => $this->input->post('ThoiGianDay'),
                'SoBuoiDay' => $this->input->post('SoBuoiDay'),
                'MucLuongYeuCau' => $this->input->post('MucLuongYeuCau'),
                'TrinhDo_id' => 1,
            );

            $this->quanly_lamgiasu_model->update($id, $data);

            // set user message
            $data['message'] = '<div class="success">update person success</div>';
            redirect('quanly/quanly_lamgiasu/index', 'refresh');
        }
    }

    function delete($id) {
        // delete person
        $this->quanly_lamgiasu_model->delete($id);

        // redirect to person list page
        redirect('quanly/quanly_lamgiasu/index', 'refresh');
    }

}

?>
